package com.kinon.market.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.kinon.market.R;
import com.kinon.market.view.DingdandetialActivity;
import com.kinon.market.view.DingdandetialerweimaActivity;


/**
 * 取餐
 */

public class QucanFragment extends Fragment {

    String str = "";
    private Button btn_9;
    private Button btn_8;
    private Button btn_7;
    private Button btn_6;
    private Button btn_5;
    private Button btn_4;
    private Button btn_3;
    private Button btn_2;
    private Button btn_1;
    private Button btn_0;
    private Button btn_00;
    private Button btn_dian;
    private Button btn_wancheng;
    private Button btn_qingchu;
    private EditText et_sousuo;
    private EditText et_id;
    private EditText et_id2;
    /**
     * 监听EditText
     */
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
//	    	Toast.makeText(getActivity(), "4", 1).show();
            if (et_id.getText().length() == 16) {
                Intent intent = new Intent();
                intent.putExtra("orderid", et_id.getText().toString());
                intent.setClass(getActivity(), DingdandetialerweimaActivity.class);
                startActivity(intent);
                et_id.setText("");
//				Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
            }


        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_qucan, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initRes();
    }

    private void initRes() {
        btn_9 = (Button) getView().findViewById(R.id.btn_9);
        btn_8 = (Button) getView().findViewById(R.id.btn_8);
        btn_7 = (Button) getView().findViewById(R.id.btn_7);
        btn_6 = (Button) getView().findViewById(R.id.btn_6);
        btn_5 = (Button) getView().findViewById(R.id.btn_5);
        btn_4 = (Button) getView().findViewById(R.id.btn_4);
        btn_3 = (Button) getView().findViewById(R.id.btn_3);
        btn_2 = (Button) getView().findViewById(R.id.btn_2);
        btn_1 = (Button) getView().findViewById(R.id.btn_1);
        btn_0 = (Button) getView().findViewById(R.id.btn_0);
        btn_00 = (Button) getView().findViewById(R.id.btn_00);
        btn_dian = (Button) getView().findViewById(R.id.btn_dian);
        btn_wancheng = (Button) getView().findViewById(R.id.btn_wancheng);
        btn_qingchu = (Button) getView().findViewById(R.id.btn_qingchu);
        et_sousuo = (EditText) getView().findViewById(R.id.et_sousuo);
        et_id = (EditText) getView().findViewById(R.id.et_id);
        et_id2 = (EditText) getView().findViewById(R.id.et_id2);


        btn_9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "9";
                et_sousuo.setText(str);
            }
        });
        btn_8.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "8";
                et_sousuo.setText(str);
            }
        });
        btn_7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "7";
                et_sousuo.setText(str);
            }
        });
        btn_6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "6";
                et_sousuo.setText(str);
            }
        });
        btn_5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "5";
                et_sousuo.setText(str);
            }
        });
        btn_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "4";
                et_sousuo.setText(str);
            }
        });
        btn_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "3";
                et_sousuo.setText(str);
            }
        });
        btn_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "2";
                et_sousuo.setText(str);
            }
        });
        btn_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "1";
                et_sousuo.setText(str);
            }
        });
        btn_0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "0";
                et_sousuo.setText(str);
            }
        });
        btn_00.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + "00";
                et_sousuo.setText(str);
            }
        });
        btn_dian.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = str + ".";
                et_sousuo.setText(str);
            }
        });
        btn_wancheng.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("dayno", str);
                intent.setClass(getActivity(), DingdandetialActivity.class);
                startActivity(intent);
                str = "";
                et_sousuo.setText(str);
            }
        });
        btn_qingchu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                str = "";
                et_sousuo.setText(str);
            }
        });

    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        et_id.requestFocus();
        et_id.addTextChangedListener(watcher);
    }

}
